package controller;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.CityRegionDAO;
import model.VenueDAO;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import beans.UserBean;
import beans.VenueBean;

/**
 * Servlet implementation class RecentVenuesServlet
 */
@WebServlet("/RecentVenuesServlet")
public class RecentVenuesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RecentVenuesServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		UserBean host = (UserBean)session.getAttribute("currentSessionUser");
		ArrayList<VenueBean> venues = VenueDAO.recentVenues(host);
		//System.out.println(venues.toString());
		Type listOfVenues = new TypeToken<List<VenueBean>>(){}.getType();
		Gson gson = new Gson();

		String s = gson.toJson(venues, listOfVenues);
		System.out.println(s);
		
		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");
        //response.getWriter().write(new Gson().toJson("prova"));
		response.getWriter().write(new Gson().toJson(venues, listOfVenues));	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
